import hmac
import hashlib

password='test123'
ki=[]
ko=[]
print "using /%s/ as the user password" % password
secret=password
if len(secret)>64:
    secret=hmac.new(secret).hexdigest()
print "using /%s/ as the shared secret" % secret

Ki=(secret+'\0'*(64-len(secret))).translate(hmac.trans_36)
Ko=(secret+'\0'*(64-len(secret))).translate(hmac.trans_5C)
print "HMAC keys\ninner=/%s/\nouter=/%s/" % (Ki, Ko)
innermd5=hashlib.new('md5')
innermd5.update(Ki)
outermd5=hashlib.new('md5')
outermd5.update(Ko)

innerhex=innermd5.hexdigest()
outerhex=outermd5.hexdigest()
print "HEX:%s\nHEX:%s\n" % (innerhex, outerhex)

hashedvalue="%s%s" % (outerhex, innerhex)
print "{CRAM-MD5}%s [%d]" % (hashedvalue, len(hashedvalue))


# cram_md5('test123')
